package com.leonwoodstudio.eventdetectionclient;

import android.app.AlertDialog;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.leonwoodstudio.eventdetectionclient.eventmessage.ParsedEventMessageToSave;
import com.leonwoodstudio.eventdetectionclient.utils.FileAccessor;
import com.leonwoodstudio.eventdetectionclient.utils.Util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * Created by win7 on 2017/4/3.
 */
public class EventHistoryFragment extends Fragment {
    private ListView event_history_list_view;
    public EventHistoryListViewAdapter event_history_list_view_adapter;
    private String[] event_types;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_event_history, container, false);
        event_history_list_view = (ListView) rootView.findViewById(R.id.event_history_list);
        event_history_list_view_adapter = new EventHistoryListViewAdapter(getActivity());
        event_history_list_view.setAdapter(event_history_list_view_adapter);
        event_history_list_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // TODO Auto-generated method stub
                ParsedEventMessageToSave parsed = (ParsedEventMessageToSave) event_history_list_view.getItemAtPosition(position);
                showEventDialog(parsed);
            }
        });
        Resources res = getResources();
        event_types = res.getStringArray(R.array.event_type);
        new EventHistoryLoadFromDbAsyncTask(this).execute();
        return rootView;
    }

    private void showEventDialog(ParsedEventMessageToSave parsed) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("事件详情");
        builder.setPositiveButton("确定", null);
        View view = View.inflate(getActivity(), R.layout.event_dialog, null);
        builder.setView(view);
        TextView message = (TextView) view.findViewById(R.id.event_dialog_message);
        String event_str = Util.DateToString(Util.StringToDate(parsed.getDate())) + "\n" + parsed.getCamera_idx() + "号摄像机拍下\n" +
                parsed.getLane_inx() + "号车道发生 " + event_types[Integer.valueOf(parsed.getEvent_type())] + " 事件\n" + "坐标:X" + parsed.getCoordinate_x() +
                ", Y" + parsed.getCoordinate_y();
        message.setText(event_str);
        if (FileAccessor.getInstance(null).isFileExist(FileAccessor.getInstance(null).getFilesDir() + parsed.getPic_name())) {
            ImageView pic = (ImageView) view.findViewById(R.id.event_dialog_pic);
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(new File(FileAccessor.getInstance(null).getFilesDir() + parsed.getPic_name()));
                Bitmap bm = BitmapFactory.decodeStream(fis);
                pic.setImageBitmap(bm);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } finally {
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        builder.setIcon(android.R.drawable.ic_dialog_info);
        builder.create().show();
    }
}
